/**
 * 卡片分类管理-add
 */
var React = require("react");
var {Col,Button,Form,FormGroup,ControlLabel,InputGroup} = require("react-bootstrap");
var {Select,RedStar,Util,FormPage} = require("flower");
//百度翻译
const appid = Util.config.baiduApi.appId;
const key = Util.config.baiduApi.key;
class Add extends FormPage{

    constructor(props){
        super(props);
        this.state = {
            addTitle : "新增卡片",
            sortList:null
        };
    }
    componentDidMount(){
        if(this.props.dataId){
            //编辑
            var param = {
                id:this.props.dataId
            }
            var ret = Util.get("/grid/getById",param);
            console.info(ret);
            this.setState({
                    addTitle:"编辑卡片"
                });
            if(this.sortList==null){
                var ret2 = Util.get("/sort/listAll");
                this.refs.sortId.setState({
                    dataSource:ret2
                })
            }
            this.setFormData(ret);
        }else{
            if(this.sortList==null){
                var ret = Util.get("/sort/listAll");
                this.refs.sortId.setState({
                    dataSource:ret
                })
            }
            //新增
            this.setState({
                addTitle:"新增卡片"
            });
        }

        console.info(this.props)
    }

    title(){
        var data = [
            {name:"首页",href:"/load/admin/Main.jsx"},
            {name:"卡片管理",href:"/load/admin/grid/Index.jsx"},
            {name:this.state.addTitle,href:""}
        ];
        return this.cbb(data);
    }
    //百度翻译
    baiduTrans(){
        var query = this.getFormData().key;
        if(!Util.isNotBlank(query)){
            alert("请输入卡片key，再执行百度翻译！");
            return false;
        }
        var salt = (new Date).getTime();
        // 多个query可以用\n连接  如 query='apple\norange\nbanana\npear'
        var reg = new RegExp("[\\u4E00-\\u9FFF]+","g");
        var from = "en";
        var to = "zh";
        if(reg.test(query)) {
            from = 'zh';
            to = "en";
        }
        var str1 = appid + query + salt +key;
        var sign = MD5(str1);
        $.ajax({
            url: 'http://api.fanyi.baidu.com/api/trans/vip/translate',
            type: 'get',
            dataType: 'jsonp',
            data: {
                q: query,
                appid: appid,
                salt: salt,
                from: from,
                to: to,
                sign: sign
            },
            success: function (data) {
                console.log(data);
                $("#valueTextarea").val(data.trans_result[0].dst);
            }
        });

    }

    content(){
        return <Form horizontal>
                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            卡片key<RedStar/>
                        </Col>
                        <Col sm={8}>

                            <InputGroup>
                                <input className="form-control" placeholder="请输入卡片key" ref="key" />
                                <InputGroup.Addon><span onClick={this.baiduTrans.bind(this)} style={{cursor:"pointer",textUnderline:"none"}}>百度翻译</span></InputGroup.Addon>
                            </InputGroup>
                            <input type="hidden" ref="id"/>
                        </Col>

                    </FormGroup>

                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            卡片value<RedStar/>
                        </Col>
                        <Col sm={8}>
                            <textarea id="valueTextarea" rows="5" className="form-control" placeholder="请输入卡片value" ref="value"></textarea>
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            卡片分类<RedStar/>
                        </Col>
                        <Col sm={4}>
                            <Select ref="sortId" dataSource={this.state.sortList}/>
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col componentClass={ControlLabel} sm={2}>
                            排序<RedStar/>
                        </Col>
                        <Col sm={2}>
                            <input type="number" className="form-control" ref="sortOrder"/>
                        </Col>
                    </FormGroup>

                    <FormGroup>
                        <Col smOffset={1} sm={8}>
                            <Button type="button" bsStyle="primary" onClick={()=>{
                                console.info(this.getFormData());
                                var data = this.getFormData();
                                if(data.id){
                                    var ret = Util.post("/grid/edit",data);

                                    if(ret.state == 200){
                                        alert("编辑成功!");
                                        Util.goto("/load/admin/grid/Index.jsx");
                                    }else if(ret.state == 300){
                                        var msg = "";
                                        for(var key in ret.msg){
                                            msg += "\r\n"+ret.msg[key];
                                        }
                                        alert("验证未通过！原因："+msg);
                                    }else{
                                        alert("编辑失败！");
                                    }

                                }else{
                                    var ret = Util.post("/grid/add",data);

                                    if(ret.state == 200){
                                        alert("新增成功!");
                                        Util.goto("/load/admin/grid/Index.jsx");
                                    }else if(ret.state == 300){
                                        var msg = "";
                                        for(var key in ret.msg){
                                            msg += "\r\n"+ret.msg[key];
                                        }
                                        alert("验证未通过！原因："+msg);
                                    }else{
                                        alert("新增失败！");
                                    }
                                }

                            }}>
                                提交
                            </Button>
                            <Button type="button" bsStyle="default" style={{marginLeft:"26px"}} onClick={()=>{
                                Util.goto("/load/admin/grid/Index.jsx");
                            }}>
                                返回列表
                            </Button>
                        </Col>
                    </FormGroup>
                </Form>

    }
}
module.exports = Add;